package com.moter.demo01.requirements;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo3 {
    static class Sale {
        public int five;
        public int ten;

        public Sale(int five, int ten) {
            this.five = five;
            this.ten = ten;

        }
    }

    static class Buyer  {
        private static Lock lock = new ReentrantLock();
        private static Condition condition = lock.newCondition();
        private static Sale sale;
        private int five;//五元的个数
        private int ten;//十元的个数
        private int twenty;//二十元的个数

        public Buyer(int five, int ten, int twenty, Sale sale) {
            this.sale = sale;
            this.five = five;
            this.ten = ten;
            this.twenty = twenty;
        }

        //用lock实现的
        public void runLock() {
            lock.lock();
            try {
                if (five >= 1) {
                    sale.five += 1;
                    System.out.println(Thread.currentThread().getName() + "买到票了");
                    condition.signalAll();
                } else if (ten >= 1) {
                    if (sale.five == 0) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "一边去，无法找零，请等一等");
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        runLock();
                    } else {
                        sale.five--;
                        sale.ten += 1;
                        System.out.println(Thread.currentThread().getName() + "买到票了");
                        condition.signalAll();
                    }
                } else if (twenty >= 1) {
                    if (sale.five >= 3) {
                        sale.five -= 3;
                        System.out.println(Thread.currentThread().getName() + "买到票了");
                        condition.signalAll();
                    } else if (sale.five >= 1 && sale.ten >= 1) {
                        sale.five--;
                        sale.ten--;
                        System.out.println(Thread.currentThread().getName() + "买到票了");
                        condition.signalAll();
                    } else {
                        try {
                            System.out.println(Thread.currentThread().getName() + "一边去，无法找零，请等一等");
                            condition.await();
                            runLock();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    System.out.println("没有钱就不要来购物");
                }
            } finally {
                lock.unlock();
            }
        }
        //用synchronized实现
        public void runSynchronized() {
            synchronized(sale) {
                if (five >= 1) {
                    sale.five += 1;
                    System.out.println(Thread.currentThread().getName() + "买到票了");
                } else if (ten >= 1) {
                    if (sale.five <= 0) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "无法找零，请等一等");
                            sale.wait();
                            runSynchronized();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        runSynchronized();
                    } else {
                        sale.five--;
                        sale.ten += 1;
                        System.out.println(Thread.currentThread().getName() + "买到票了");
                    }
                } else if (twenty >= 1) {
                    if (sale.five >= 3) {
                        sale.five -= 3;
                        System.out.println(Thread.currentThread().getName() + "买到票了");

                    } else if (sale.five >= 1 && sale.ten >= 1) {
                        sale.five--;
                        sale.ten--;
                        System.out.println(Thread.currentThread().getName() + "买到票了");
                    } else {
                        try {
                            System.out.println(Thread.currentThread().getName() + "无法找零，请等一等");
                            sale.wait();
                            runSynchronized();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    System.out.println("没有钱就不要来购物");
                }
                sale.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        Sale sale = new Sale(1, 1);
        Buyer wang = new Buyer(0, 1, 0, sale);
        Buyer han = new Buyer(0, 0, 1, sale);
        Buyer A = new Buyer(1, 0, 0, sale);
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
            han.runSynchronized();
        }, "韩老师");
        Thread thread2 = new Thread(() -> {

            System.out.println(Thread.currentThread().getName());
            wang.runSynchronized();
        }, "王越强");
        thread2.start();
        thread.start();
        Thread thread1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
            A.runSynchronized();
        }, "A");
        thread1.start();
        while(Thread.activeCount()>2){}
        System.out.println(sale.five+" "+sale.ten);
    }
}
